<?php
/**
 * @package Waterwheel
 */
/* 
 *  Copyright 2010 Andrew Rickmann
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */


/**
 * This plugin will contain the help files for using waterwheel.
 *
 * @author Andrew Rickmann
 */
class gettingstrated {

    /**
     *
     * Plugin to the request rules action so another request rule can be added.
     *
     * @param request $request
     */
    function action_request_rules( request $request ){

	//add the rule to handle getting started files.
	$request->add_request_rule('gettingstarted-folderandfile', array($this,'request_handler'), 'gettingstarted/{data:folder}/{data:file}','Getting Started - Folder and file paths');
	$request->add_request_rule('gettingstarted-file', array($this,'request_handler'), 'gettingstarted/{data:file}', 'Getting started file path only');
	$request->add_request_rule('gettingstarted-index', array($this,'request_handler'), 'gettingstarted' , 'Getting started index file');

    }

    /**
     * The request handler. This processes the request vars and decides which files to load
     * In this case it is a very simple handler that transforms the path into a file name
     * and includes it.
     *
     * There is no database access.
     *
     *
     * @param request $request
     */
    function request_handler( request $request ){

	//create some variables that we can use in the included files
	$plugin_url = APPLICATION_URL . '/plugins/gettingstarted';
	$home = APPLICATION_URL . '/gettingstarted/';

	//include the geshi code highlighter
	include_once(APPLICATION_PATH . '/plugins/gettingstarted/files/geshi/geshi.php');

	//create a geshi object to use
	$geshi = new GeSHi( '' , 'php');
	    //turn on numbering
	    $geshi->set_header_type(GESHI_HEADER_DIV);
	    $geshi->set_overall_style('border:1px solid #efefef; padding:10px 0 0 0; font-size:1.1em; margin-bottom:1em;');
	    $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS , 2);
	    $geshi->set_line_style('background: #fffff;', 'background: #fafafa;');
	   

	//work out which getting started page is being asked for
	if ( isset( $request->request_params['folder'] ) ) {
	    $file = $request->request_params['folder'] . '/' . $request->request_params['file'] . '.php';
	} else if ( isset( $request->request_params['file'] ) ) {
	    $file = $request->request_params['file'] . '.php';
	} else {
	    $file = 'index.php';
	}

	//check the file exists
	if ( file_exists( APPLICATION_PATH . '/plugins/gettingstarted/files/' . $file ) ){
	    //load it
	    include( APPLICATION_PATH . '/plugins/gettingstarted/files/' . $file );
	} else {
	    //load the index file
	    include( APPLICATION_PATH . '/plugins/gettingstarted/files/index.php' );
	}
    }
}
?>